package com.flatlyapps.shoppinglist;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;

import com.flatlyapps.shoppinglist.R;
import com.neopixl.pixlui.components.button.Button;
import com.neopixl.pixlui.components.edittext.EditText;
import com.neopixl.pixlui.components.textview.TextView;
import com.qustom.dialog.QustomDialogBuilder;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

public class EditItemDialogFragment extends DialogFragment{

	private static final String EDIT_TEXT_NAME_STRING = "name";
	private static final String EDIT_TEXT_COST_STRING = "cost";
	private EditText editTextName;
	private EditText editTextCost;
	private TextView dialogTitle;
	private View dividerView;
	private Button okButton;

	long itemId;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.edit_item, null);
		editTextName = (EditText) view.findViewById(R.id.edit_name);
		editTextCost = (EditText) view.findViewById(R.id.edit_cost);
		editTextCost.addTextChangedListener(new DecimalPlaceListener(2));
		dialogTitle = (TextView) view.findViewById(R.id.dialog_title);
		dividerView = (View) view.findViewById(R.id.divider_view);
		okButton = (Button) view.findViewById(R.id.ok_button);

		//getting relevant items
		itemId = getArguments().getLong(ShoppingListFragment.ITEM_ID_KEY);
		String itemName = getArguments().getString(ShoppingListFragment.ITEM_NAME_KEY);

		int color = getResources().getColor(((ShoppingListFragment) getTargetFragment()).getColour());

		dialogTitle.setText("Edit " + itemName);
		dividerView.setBackgroundColor(color);
		okButton.setTextColor(color);

		final AlertDialog dialog = new QustomDialogBuilder(getActivity()).create();
		dialog.setView(view);
		view.findViewById(R.id.ok_button).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
					String name = editTextName.getText().toString();
					String cost = editTextCost.getText().toString();
					DecimalFormat df = new DecimalFormat("#0.00");
					df.setParseBigDecimal(true);
					try {
						if(cost.trim().isEmpty() && !name.trim().isEmpty()){
							((ShoppingListFragment) getTargetFragment()).editName(itemId, name);
						}else if(name.trim().isEmpty() && !cost.trim().isEmpty()){
							((ShoppingListFragment) getTargetFragment()).addValue(itemId, (BigDecimal) df.parse(cost));
						}else if(!name.trim().isEmpty() && !cost.trim().isEmpty()){
							((ShoppingListFragment) getTargetFragment()).editNameAndValue(itemId, name, (BigDecimal) df.parse(cost));
						}
					} catch (ParseException e) {
						throw new NumberFormatException(e.getMessage());
					}
				dialog.dismiss();
				((ShoppingListFragment) getTargetFragment()).editIsOpen = false;
			}
		});
		view.findViewById(R.id.cancel_button).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				((ShoppingListFragment) getTargetFragment()).editIsOpen = false;
			}
		});
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		return dialog;
	}

	@Override
	public void onSaveInstanceState(Bundle arg0) {
		//saving the edit text
		arg0.putString(EDIT_TEXT_NAME_STRING, editTextName.getText().toString());
		arg0.putString(EDIT_TEXT_COST_STRING, editTextCost.getText().toString());
		super.onSaveInstanceState(arg0);
	}
}
